//
//  UIManagement.m
//  51-Tour
//
//  Created by xiang ying on 14-3-19.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "UIManagement.h"
#import "PKRevealController.h"
#import "LeftMenuViewController.h"
#import "RightMenuViewController.h"
#import "CenterMapViewController.h"
#import "ShareViewController.h"
#import "AboutViewController.h"
#import "MoreViewController.h"
#import "CityListViewController.h"
#import "PersonInfoViewController.h"
#import "HistoryViewController.h"
#import "RecordViewController.h"
#import "RegisterViewController.h"
#import "UserDTO.h"
#import "AdWebViewController.h"
#import "db_Downloading.h"

typedef enum {
    Tag_Unknow      = -1,
    Tag_Download    =  0,
    Tag_Share,
    Tag_About,
    Tag_More,
    Tag_AD,
    Tag_Recommend   = 10,
    Tag_Personinfo,
    Tag_History,
    Tag_Record,
}VIEW_TAG;

static UIManagement *uimanage = nil;

@interface UIManagement(){
    LeftMenuViewController *_leftMenu;
    RightMenuViewController *_rightMenu;
    CenterMapViewController *_centerMap;
    UINavigationController *_navCenter;
    CityListViewController *_cityList;
    
    PKRevealController      *_pk;
    
    VIEW_TAG                flag;
}

@end

@implementation UIManagement

+(instancetype)shareInstance{
    if (!uimanage) {
        uimanage = [[self alloc] init];
    }
    return uimanage;
}

- (void)dealloc{
    [db_Downloading closeDatabase];
}

-(instancetype)init{
    self = [super init];
    if (self) {
       
       _leftMenu = [[LeftMenuViewController alloc] init];
       _rightMenu = [[RightMenuViewController alloc] init];
       _centerMap = [[CenterMapViewController alloc] init];
       _navCenter = [[UINavigationController alloc] initWithRootViewController:_centerMap];

        _pk = [[PKRevealController alloc] initWithFrontViewController:_navCenter leftViewController:_leftMenu rightViewController:_rightMenu options:nil];
        
        [[[UIApplication sharedApplication] delegate] window].rootViewController = _pk;
        
        flag = Tag_Unknow;
        
        [db_Downloading openDataBase];
    }
    return self;
}

-(UIViewController*)leftMenu{
    return _leftMenu;
}

-(UIViewController*)rightMenu{
    return _rightMenu;
}

-(void)setPKUseful:(BOOL)useful{
    [_pk setPKRespondUserAction:useful];
}

#pragma mark - 页面之间的跳转
-(void)showLeft{
    flag = Tag_Unknow;
    [_pk showViewController:_pk.leftViewController];
}

-(void)showRight{
    flag = Tag_Unknow;
    if (![DataCenter instance].user) {
        RegisterViewController *registerView = [[RegisterViewController alloc] init];
        [_navCenter pushViewController:registerView animated:YES];
    }else{
        _pk.rightViewController = _rightMenu;
        [_pk showViewController:_pk.rightViewController];
    }
}

-(void)showCenter{
    flag = Tag_Unknow;
    [_pk showViewController:_pk.frontViewController];
}

-(void)popCenter{
    [_navCenter popToRootViewControllerAnimated:NO];
}

//离线包
-(void)showDownload{
    if (!_cityList) {
         _cityList = [[CityListViewController alloc] init];
    }
    if (![DataCenter instance].user) {
        [self showCenter];
        [self showRight];
        return;
    }
    if (flag != Tag_Download) {
        [self popCenter];
        [_navCenter pushViewController:_cityList animated:NO];
        flag = Tag_Download;
    }
    [_pk showViewController:_pk.frontViewController];
}

//广告
-(void)showAD{
//    if (flag!=Tag_AD) {
//        [self popCenter];
//        AdWebViewController *_ad = [[AdWebViewController alloc] init];
//        [_navCenter pushViewController:_ad animated:YES];
//        flag = Tag_AD;
//    }
//    [_pk showViewController:_pk.frontViewController];
    NSURL *url = [NSURL URLWithString:[DataCenter instance].adUrl];
    [[UIApplication sharedApplication] openURL:url];
}

//分享
-(void)showShare{
    if (flag!=Tag_Share) {
        [self popCenter];
        ShareViewController *_share = [[ShareViewController alloc] init];
        [_navCenter pushViewController:_share animated:NO];
        flag = Tag_Share;
    }
    [_pk showViewController:_pk.frontViewController];
}

//关于
-(void)showAbout{
    if (flag!=Tag_About) {
        [self popCenter];
        AboutViewController *_about = [[AboutViewController alloc] init];
        [_navCenter pushViewController:_about animated:NO];
        flag = Tag_About;
    }
    [_pk showViewController:_pk.frontViewController];
}

//更多
-(void)showMore{
    if (flag!=Tag_More) {
        [self popCenter];
        MoreViewController *_more = [[MoreViewController alloc] init];
        [_navCenter pushViewController:_more animated:NO];
        flag = Tag_More;
    }
    [_pk showViewController:_pk.frontViewController];
}

//个人资料
-(void)showPersoninfo{
    if (flag!=Tag_Personinfo) {
        [self popCenter];
        PersonInfoViewController *_personInfo = [[PersonInfoViewController alloc] init];
        [_navCenter pushViewController:_personInfo animated:NO];
        flag = Tag_Personinfo;
    }
    [_pk showViewController:_pk.frontViewController];
}

//历史信息
-(void)showHistory{
    if (flag!=Tag_History) {
        [self popCenter];
        HistoryViewController *_history = [[HistoryViewController alloc] init];
        [_navCenter pushViewController:_history animated:NO];
        flag = Tag_History;
    }
    [_pk showViewController:_pk.frontViewController];
}

//讲解记录
-(void)showRecord{
    if (flag!=Tag_Record) {
        [self popCenter];
        RecordViewController *_record = [[RecordViewController alloc] init];
        [_navCenter pushViewController:_record animated:NO];
        flag = Tag_Record;
    }
    [_pk showViewController:_pk.frontViewController];
}
@end
